//
//  MemberLastlyWelfareTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/30.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "MemberLastlyWelfareTableViewCell.h"
#import "MemberWelfareLastlyCollectionViewCell.h"
#import "PopupWebView.h"
#import <AudioToolbox/AudioToolbox.h>
static NSString *memberLastlyWelfareCellID = @"memberLastlyWelfareCellID";

@interface MemberLastlyWelfareTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    SystemSoundID scoreClickBtnID;   // 自己设置的音效 (系统的振动音效 - kSystemSoundID_Vibrate)
}
// 头部
@property (strong, nonatomic) UIImageView *bgIV;
@property (strong, nonatomic) UIImageView *exclusiveIV;
@property (strong, nonatomic) UILabel *bigL;
@property (strong, nonatomic) UILabel *desL;

// 趣享淘会员大背景
@property (strong, nonatomic) UIImageView *listBgIV;

// 趣享淘会员标题，副标题部分
@property (strong, nonatomic) UILabel *titleL;
@property (strong, nonatomic) UIImageView *leftIV;
@property (strong, nonatomic) UIImageView *rightIV;
@property (strong, nonatomic) UILabel *subTitleL;

// 商品列表部分
@property (strong, nonatomic) UICollectionView *goodsCollectionView;

@property (nonatomic, strong) NSArray *dataArray;


@end

@implementation MemberLastlyWelfareTableViewCell


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.bgIV];
        [self.contentView addSubview:self.exclusiveIV];
        [self.contentView addSubview:self.bigL];
        [self.contentView addSubview:self.desL];
        
        [self.contentView addSubview:self.listBgIV];
        
        [self.contentView addSubview:self.titleL];
        [self.contentView addSubview:self.leftIV];
        [self.contentView addSubview:self.rightIV];
        [self.contentView addSubview:self.subTitleL];
        [self.contentView addSubview:self.goodsCollectionView];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"5a2e388b2886c.wav" ofType:@""]]), &scoreClickBtnID);
        [self setupUI];
    }
    return self;
}

//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
//
//    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//
//
//    }
//
//    return self;
//}

#pragma mark -delegate \ datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MemberWelfareLastlyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:memberLastlyWelfareCellID forIndexPath:indexPath];
    cell.goodListModel = self.dataArray[indexPath.row];
    cell.parentVC = self.parentVC;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    PopupWebView *popppview = [[PopupWebView alloc] init];
    popppview.hidden = YES;
    popppview.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [[UIApplication sharedApplication].keyWindow addSubview:popppview];
    
    SupportCategoryGoodListModel *model = self.dataArray[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"{\"user_id\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey]];
    [PPNetworkHelper POST:@"welfare/browse/index" parameters:@{@"json":string} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            [popppview loadWithHtml:responseObject[@"data"][@"jiangli"]];
            popppview.hidden = NO;
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
            AudioServicesPlaySystemSound(self->scoreClickBtnID);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                [popppview removeFromSuperview];
                NSDictionary *dict = @{@"goodId":model.goodId};
                [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
            });
        } else {
            NSDictionary *dict = @{@"goodId":model.goodId};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
        }
    } failure:^(NSError *error) {
        NSDictionary *dict = @{@"goodId":model.goodId};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
    }];
    
   
}

- (void)setupUI {
    
    __weak typeof(self) weakSelf = self;
    [self.bgIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left);
        make.right.mas_equalTo(weakSelf.contentView.mas_right);
        make.top.mas_equalTo(weakSelf.contentView.mas_top);
        make.height.mas_equalTo(jisuan(290));
    }];
    
    [self.exclusiveIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.bgIV.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.bgIV.mas_centerY);
        make.width.mas_equalTo(jisuan(360));
        make.height.mas_equalTo(jisuan(200));
        
    }];
    
    [self.bigL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.exclusiveIV.mas_centerX);
        make.top.mas_equalTo(weakSelf.exclusiveIV.mas_top).with.offset(jisuan(40));
    }];
    [self.desL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.exclusiveIV.mas_centerX);
        make.bottom.mas_equalTo(weakSelf.exclusiveIV.mas_bottom).with.offset(-jisuan(34));
    }];
    [self.listBgIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.bgIV.mas_centerX);
        make.top.mas_equalTo(weakSelf.bgIV.mas_bottom).with.offset(jisuan(20));
        make.width.mas_equalTo(jisuan(750));
        make.height.mas_equalTo(jisuan(776));
    }];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.listBgIV.mas_centerX);
        make.top.mas_equalTo(weakSelf.listBgIV.mas_top).with.offset(jisuan(40));
 
    }];
    [self.subTitleL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.listBgIV.mas_centerX);
        make.top.mas_equalTo(weakSelf.titleL.mas_bottom).with.offset(jisuan(20));
        
    }];
    
    [self.leftIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.subTitleL.mas_left).with.offset(-jisuan(10));
        make.centerY.mas_equalTo(weakSelf.subTitleL.mas_centerY);
        make.width.mas_equalTo(jisuan(60));
        make.height.mas_equalTo(jisuan(4));
        
    }];
    [self.rightIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.subTitleL.mas_right).with.offset(jisuan(10));
        make.centerY.mas_equalTo(weakSelf.subTitleL.mas_centerY);
        make.width.mas_equalTo(jisuan(60));
        make.height.mas_equalTo(jisuan(4));
        
    }];
    
    [self.goodsCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.listBgIV.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(weakSelf.subTitleL.mas_bottom).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.listBgIV.mas_right);
        make.height.mas_equalTo(jisuan(542));
        
    }];
    
}

#pragma mark -lazy

- (UILabel *)titleL {
    
    if (!_titleL) {
        
        _titleL = [[UILabel alloc] init];
        _titleL.textColor = Color333333;
        _titleL.text = @"爱淘帮会员";
        _titleL.textAlignment = NSTextAlignmentCenter;
        _titleL.font = [UIFont fontWithName:@"MFYueYuan_Noncommercial-Regular" size:jisuan(56)];
        
    }
    
    return _titleL;
}

- (UIImageView *)leftIV {
    
    if (!_leftIV) {
        
        _leftIV = [[UIImageView alloc] init];
        _leftIV.contentMode = UIViewContentModeScaleAspectFit;
        _leftIV.image = [UIImage imageNamed:@"welfareLastLeftBG"];
        
    }
    
    return _leftIV;
}

- (UIImageView *)rightIV {
    
    if (!_rightIV) {
        
        _rightIV = [[UIImageView alloc] init];
        _rightIV.contentMode = UIViewContentModeScaleAspectFit;
        _rightIV.image = [UIImage imageNamed:@"welfareLastRightBG"];
        
    }
    
    return _rightIV;
}

- (UILabel *)subTitleL {
    
    if (!_subTitleL) {
        
        _subTitleL = [[UILabel alloc] init];
        _subTitleL.textColor = Color333333;
        _subTitleL.text = @"每日准时上新";
        _subTitleL.textAlignment = NSTextAlignmentCenter;
        _subTitleL.font = Font(28);
        
    }
    
    return _subTitleL;
}

- (UICollectionView *)goodsCollectionView {
    
    if (!_goodsCollectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(jisuan(400), jisuan(540));
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = jisuan(40);

        _goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _goodsCollectionView.backgroundColor = [UIColor clearColor];
        
        _goodsCollectionView.bounces = NO;
        
        _goodsCollectionView.delegate = self;
        _goodsCollectionView.dataSource = self;
        
        _goodsCollectionView.showsVerticalScrollIndicator = NO;
        _goodsCollectionView.showsHorizontalScrollIndicator = NO;
        
        [_goodsCollectionView registerClass:[MemberWelfareLastlyCollectionViewCell class] forCellWithReuseIdentifier:memberLastlyWelfareCellID];
        
        if (@available(iOS 11.0, *)) {
            
            _goodsCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
        
    }
    
    return _goodsCollectionView;
    
}

- (UIImageView *)bgIV {
    
    if (!_bgIV) {
        
        _bgIV = [[UIImageView alloc] init];
        _bgIV.contentMode = UIViewContentModeScaleAspectFit;
        _bgIV.image = [UIImage imageNamed:@"lastWelfareBG"];
        
    }
    
    return _bgIV;
}
- (UIImageView *)exclusiveIV {
    
    if (!_exclusiveIV) {
        
        _exclusiveIV = [[UIImageView alloc] init];
        _exclusiveIV.contentMode = UIViewContentModeScaleAspectFit;
        _exclusiveIV.image = [UIImage imageNamed:@"exclusiveGoodBG"];
        
    }
    
    return _exclusiveIV;
}

- (UILabel *)bigL {
    
    if (!_bigL) {
        
        _bigL = [[UILabel alloc] init];
//        _bigL.text = @"专属商品";
        NSString *str = @"专属商品";
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attributeStr addAttribute:NSKernAttributeName value:@(jisuan(5)) range:NSMakeRange(0, str.length)];
        _bigL.attributedText = attributeStr;
        
        
        _bigL.textColor = WhiteColor;
        _bigL.font = Font(56);
        _bigL.textAlignment = NSTextAlignmentCenter;
        
    }
    return _bigL;
}

- (UILabel *)desL {
    
    if (!_desL) {
        
        _desL = [[UILabel alloc] init];
        _desL.text = @"为挚爱精选  为格调买单";
        
//        NSString *str = @"为挚爱精选  为格调买单";
//        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
//        [attributeStr addAttribute:NSKernAttributeName value:@(jisuan(3)) range:NSMakeRange(0, str.length)];
//        _desL.attributedText = attributeStr;
        
        _desL.textColor = WhiteColor;
        _desL.font = Font(24);
        _desL.textAlignment = NSTextAlignmentCenter;
        
    }
    return _desL;
}

- (UIImageView *)listBgIV {
    
    if (!_listBgIV) {
        _listBgIV = [[UIImageView alloc] init];
        _listBgIV.contentMode = UIViewContentModeScaleAspectFit;
        _listBgIV.image = [UIImage imageNamed:@"welfareLastGoodbg"];
    }
    
    return _listBgIV;
}

- (void)reloadDataWithArray:(NSArray *)array {
    
    self.dataArray = array;
    [self.goodsCollectionView reloadData];
}



@end
